package com.lwd.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwd.entity.Admin;
import com.lwd.entity.GoodsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lwd.entity.Goods;
import com.lwd.entity.Type;
import com.lwd.mapper.GoodsMapper;
import com.lwd.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsMapper goodsmapper;

	@Override
	public List<Goods> getAllGoods() {

		List<Goods> list = goodsmapper.getAllGoods();
		return list;
	}

	@Override
	public List<Type> queryTypeAll() {
		return goodsmapper.queryTypeAll();
	}

	@Override
	public boolean deleteGoodsById(Integer id) {

		int result = goodsmapper.deleteByPrimaryKey(id);
		return result > 0 ? true : false;
	}

	@Override
	public boolean updateGoods(Goods goods) {
		int result = goodsmapper.updateByPrimaryKeySelective(goods);
		return result > 0 ? true : false;
	}

	@Override
	public boolean addeGoods(Goods goods) {
		int result = goodsmapper.insertSelective(goods);
		return result > 0 ? true : false;
	}

	@Override
	public Goods queryGoodsById(Integer id) {
		return goodsmapper.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo<Goods> getPageInfo(Integer tid, String gname, Integer pageNum, Integer pageSize) {
		// 1.调用PageHelper的静态方法开启分页功能
		// 这里充分体现了PageHelper的“非侵入式”设计：原本要做的查询不必有任何修改
		PageHelper.startPage(pageNum, pageSize);

		// 2.执行查询
		List<Goods> list = goodsmapper.selectByTIdAndGname(tid,gname);

		// 3.封装到PageInfo对象中
		return new PageInfo(list);
	}

	@Override
	public Goods queryGoodsDetailById(Integer id) {
		Goods goods=goodsmapper.queryGoodsDetailById(id);
		return goods;
	}

	@Override
	public int updateFileUri(Goods goods) {
//		GoodsExample goodsExample = new GoodsExample();
//		GoodsExample.Criteria criteria = goodsExample.createCriteria();
//		criteria.andGid
		return goodsmapper.updateByPrimaryKeySelective(goods);
	}

	@Override
	public List<Goods> getMoreGoodsByTid(String tid) {
		GoodsExample goodsExample = new GoodsExample();
		GoodsExample.Criteria criteria = goodsExample.createCriteria();
		criteria.andTidEqualTo(Integer.parseInt(tid));

		List<Goods> goodsList = goodsmapper.selectByExample(goodsExample);

		return goodsList;
	}

	@Override
	public int getgetMoreGoodsByTidCount(String tid) {
		GoodsExample goodsExample = new GoodsExample();
		GoodsExample.Criteria criteria = goodsExample.createCriteria();
		criteria.andTidEqualTo(Integer.parseInt(tid));
		return goodsmapper.countByExample(goodsExample);
	}

}
